﻿package src.states{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import src.game.managers.SoundManager;
	import src.sound.SoundPlayer;

	public class EndLevel extends GameState {
		private var endLevelMC:EndLevelMc;
		
		public function EndLevel(display:DisplayObjectContainer, gameStates:GameStatesMachine) {
			super(display, gameStates);
		}
		
		public override function init():void {
			super.init();
			endLevelMC = new EndLevelMc();
			endLevelMC.addEventListener(Event.COMPLETE, disposeEvent);
			displayContent.addChild(endLevelMC);
			playSound();
		}
		
		private function playSound():void {
			var sm:SoundManager = SoundManager.getInstance();
			sm.add(new SoundPlayer(new LevelFinished()));
		}
		
		private function disposeEvent(event:Event):void {
			gameStates.setCurrentState(gameStates.getNewLevelState());
		}
		
		public override function dispose():void {
			super.dispose();
			displayContent.removeChild(endLevelMC);
			endLevelMC.removeEventListener(Event.COMPLETE, disposeEvent);
			endLevelMC = null;
		}
	}
}